package com.wang.tool.monitor.log;

import com.alibaba.fastjson.JSON;
import com.wang.tool.monitor.LinkLogUtil;
import com.wang.tool.monitor.core.LinkInfo;
import com.wang.tool.util.SimpleLogUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.function.Consumer;

/**
 * @Description 简易实现
 * @Author shaopeng
 * @Date 2022/11/19
 * @Version 1.0
 */
public class SimpleLogApi implements LogApi {

    /**
     * digest日志
     */
    public static final Logger DIGEST_LOGGER = LoggerFactory.getLogger("S_DIGEST");

    /**
     * tag日志
     */
    public static final Logger TAG_LOGGER = LoggerFactory.getLogger("S_TAG");

    /**
     * link日志
     */
    public static final Logger LINK_LOGGER = LoggerFactory.getLogger("S_LINK");


    @Override
    public Consumer<LogInfo> getLogger(String loggerName, LinkInfo linkInfo) {
        if (loggerName == null) {
            return null;
        }
        switch (loggerName) {
            case LinkLogUtil.INFO:
                if (isDowngradeInfo(linkInfo)) {
                    return null;
                }
                return logInfo -> {
                    SimpleLogUtil.BIZ_LOGGER.info(LinkLogUtil.currentLink(logInfo.getLinkInfo()) + logInfo.getContent() + " [attachment]: " + mapToString(logInfo.getAttachment()));
                };
            case LinkLogUtil.DIGEST:
                if (!DIGEST_LOGGER.isInfoEnabled()) {
                    return null;
                }
                return logInfo -> {
                    DIGEST_LOGGER.info(LinkLogUtil.currentLink(logInfo.getLinkInfo()) + logInfo.getContent() + " [attachment]: " + mapToString(logInfo.getAttachment()));
                };
            case LinkLogUtil.TAG:
                if (!TAG_LOGGER.isInfoEnabled()) {
                    return null;
                }
                return logInfo -> {
                    TAG_LOGGER.info(LinkLogUtil.currentLink(logInfo.getLinkInfo()) + "[T]" + logInfo.getContent() + " [attachment]: " + mapToString(logInfo.getAttachment()));
                };
            case LinkLogUtil.WARN:
                if (isDowngradeWarn(linkInfo)) {
                    return null;
                }
                return logInfo -> {
                    SimpleLogUtil.BIZ_LOGGER.warn(LinkLogUtil.currentLink(logInfo.getLinkInfo())  + logInfo.getContent() + " [attachment]: " + mapToString(logInfo.getAttachment()));
                };
            case LinkLogUtil.PROFILER:
                if (!LINK_LOGGER.isInfoEnabled()) {
                    return null;
                }
                return logInfo -> {
                    Map<String, Object> attachment = logInfo.getAttachment();
                    LINK_LOGGER.info(LinkLogUtil.currentLink(logInfo.getLinkInfo()) + logInfo.getContent() + "[P]" + attachment.get(LinkLogUtil.PROFILER_));
                };
            case LinkLogUtil.LINK:
                if (!LINK_LOGGER.isInfoEnabled()) {
                    return null;
                }
                return logInfo -> {
                    Map<String, Object> attachment = logInfo.getAttachment();
                    LINK_LOGGER.info(LinkLogUtil.currentLink(logInfo.getLinkInfo()) + logInfo.getContent() + "[L]" + attachment.get(LinkLogUtil.LINK_));
                };
            default:
                if (isDowngradeLogger(loggerName, linkInfo)) {
                    return null;
                }
                return logInfo -> {
                    SimpleLogUtil.BIZ_LOGGER.info(loggerName + LinkLogUtil.currentLink(logInfo.getLinkInfo()) + logInfo.getContent() + " [attachment]:" + mapToString(logInfo.getAttachment()));
                };
        }
    }


    /**
     * 自定义logger降级
     *
     * @param loggerName 日志名
     * @param linkInfo   可能为空
     * @return
     */
    protected boolean isDowngradeLogger(String loggerName, LinkInfo linkInfo) {
        return !SimpleLogUtil.BIZ_LOGGER.isInfoEnabled();
    }

    /**
     * info降级
     *
     * @param linkInfo 可能为空
     * @return
     */
    protected boolean isDowngradeInfo(LinkInfo linkInfo) {
        return !SimpleLogUtil.BIZ_LOGGER.isInfoEnabled();
    }


    /**
     * warn降级
     *
     * @param linkInfo 可能为空
     * @return
     */
    protected boolean isDowngradeWarn(LinkInfo linkInfo) {
        return !SimpleLogUtil.BIZ_LOGGER.isWarnEnabled();
    }


    @Override
    public void error(Throwable e, String errorCode, LogInfo logInfo) {
        SimpleLogUtil.BIZ_LOGGER.error(errorCode + logInfo.getContent() + " [attachment]:" + mapToString(logInfo.getAttachment()), e);
    }

    protected String mapToString(Map<String, Object> obj) {
        return obj == null ? "-" : JSON.toJSONString(obj);
    }

}
